package com.lee.lease.web.app.service.service.impl;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lee.lease.common.threadlocal.LoginUserContext;
import com.lee.lease.model.entity.*;
import com.lee.lease.web.app.mapper.*;
import com.lee.lease.web.app.service.RoomInfoService;
import com.lee.lease.web.app.vo.apartment.ApartmentItemVo;
import com.lee.lease.web.app.vo.attr.AttrValueVo;
import com.lee.lease.web.app.vo.fee.FeeValueVo;
import com.lee.lease.web.app.vo.graph.GraphVo;
import com.lee.lease.web.app.vo.room.RoomDetailVo;
import com.lee.lease.web.app.vo.room.RoomItemVo;
import com.lee.lease.web.app.vo.room.RoomQueryVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
@Slf4j
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {

    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private AttrValueMapper attrValueMapper;
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    private PaymentTypeMapper paymentTypeMapper;
    @Autowired
    private FeeValueMapper feeValueMapper;
    @Autowired
    private LeaseTermMapper leaseTermMapper;
    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;

    @Autowired
    private BrowsingHistoryMapper browsingHistoryMapper;
    @Autowired
    private LeaseAgreementMapper leaseAgreementMapper;


    @Override
    public IPage<RoomItemVo> findRoomItem(IPage<RoomItemVo> page, RoomQueryVo queryVo) {
        return baseMapper.findRoomItem(page, queryVo);
    }

    @Override
    public IPage<RoomItemVo> findRoomItemByApartmentId(IPage<RoomItemVo> page, Integer id) {
        return baseMapper.findRoomItemByApartmentId(page, id);
    }

    @Override
    public RoomDetailVo getDetailById(Integer id) {
        RoomDetailVo roomInfo = baseMapper.selectRoomById(id);
        if (roomInfo == null) {
            return null;
        }

        ApartmentItemVo  apartmentItemVo=apartmentInfoMapper.findApartmentInfoByRoomId(id);

        List<GraphVo> graphVoList = graphInfoMapper.findGraphByRoomId(id);
        List<AttrValueVo> attrValueVoList = attrValueMapper.findAttrValueByRoomId(id);
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.findFacilityByRoomId(id);
        List<LabelInfo> labelInfoList = labelInfoMapper.findLabelByRoomId(id);
        List<PaymentType> paymentTypeList = paymentTypeMapper.findPaymentTypeByRoomId(id);
        List<FeeValueVo> feeValueVoList = feeValueMapper.findFeeValueByApartmentId(apartmentItemVo.getId());
        List<LeaseTerm> leaseTermList = leaseTermMapper.findLeaseTermByRoomId(id);


        roomInfo.setApartmentItemVo(apartmentItemVo);
        roomInfo.setGraphVoList(graphVoList);
        roomInfo.setFacilityInfoList(facilityInfoList);
        roomInfo.setAttrValueVoList(attrValueVoList);
        roomInfo.setLabelInfoList(labelInfoList);
        roomInfo.setPaymentTypeList(paymentTypeList);
        roomInfo.setFeeValueVoList(feeValueVoList);
        roomInfo.setLeaseTermList(leaseTermList);

        //todo 最后保存一条浏览记录
        browsingHistoryMapper.saveHistory(id, LoginUserContext.getLoginUser().getUserId());

        return roomInfo;
    }
    @Override
    public RoomDetailVo getDetailById1(Integer id) {
        RoomInfo roomInfo = baseMapper.selectRoomById1(id);
        if (roomInfo == null) {
            return null;
        }
        RoomDetailVo roomDetailVo = new RoomDetailVo();
        BeanUtils.copyProperties(roomInfo, roomDetailVo);

        ApartmentItemVo  apartmentItemVo=apartmentInfoMapper.findApartmentInfoByRoomId(id);

        List<GraphVo> graphVoList = graphInfoMapper.findGraphByRoomId(id);
        List<AttrValueVo> attrValueVoList = attrValueMapper.findAttrValueByRoomId(id);
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.findFacilityByRoomId(id);
        List<LabelInfo> labelInfoList = labelInfoMapper.findLabelByRoomId(id);
        List<PaymentType> paymentTypeList = paymentTypeMapper.findPaymentTypeByRoomId(id);
        List<FeeValueVo> feeValueVoList = feeValueMapper.findFeeValueByApartmentId(apartmentItemVo.getId());
        List<LeaseTerm> leaseTermList = leaseTermMapper.findLeaseTermByRoomId(id);
        Boolean isChickIn = leaseAgreementMapper.selectChickIn(id);




        roomDetailVo.setApartmentItemVo(apartmentItemVo);
        roomDetailVo.setGraphVoList(graphVoList);
        roomDetailVo.setFacilityInfoList(facilityInfoList);
        roomDetailVo.setAttrValueVoList(attrValueVoList);
        roomDetailVo.setLabelInfoList(labelInfoList);
        roomDetailVo.setPaymentTypeList(paymentTypeList);
        roomDetailVo.setFeeValueVoList(feeValueVoList);
        roomDetailVo.setLeaseTermList(leaseTermList);
        roomDetailVo.setIsCheckIn(isChickIn);

        //todo 最后保存一条浏览记录
        browsingHistoryMapper.saveHistory(id, LoginUserContext.getLoginUser().getUserId());

        return roomDetailVo;
    }
}




